using UnityEngine;

public class FollowTargetCamera : MonoBehaviour
{
    // 目标游戏对象
    public Transform target;

    // 跟随速度
    public float smoothSpeed = 0.125f;

    // 相对于目标的偏移量
    public Vector3 offset = new Vector3(0, 1, -1); // 使摄像机位于目标上方一点，稍微远离目标

    private void LateUpdate()
    {
        // 如果目标为空，则抛出异常
        if (target == null)
        {
            Debug.LogError("The variable target of FollowTargetCamera has not been assigned.");
            return;
        }

        // 计算目标位置
        Vector3 desiredPosition = target.position + offset;

        // 平滑跟随
        transform.position = Vector3.Lerp(transform.position, desiredPosition, smoothSpeed);
    }
}